home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_065 / mwb / mwb.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  264 lines

  1.  
  2. /*
  3.  *  Multi-WorkBench-Screens program.
  4.  *
  5.  *  (c) 1987  Matthew Dillon, All rights reserved
  6.  *  Freely Distributable.
  7.  *
  8.  *  AZTEC only, must compile with +L option.  MWB_RESIDENT must be compiled
  9.  *  with +BCDL options.
  10.  *
  11.  *  See MWB.DOC for more information.
  12.  */
  13.  
  14. #include "mwb.h"
  15. #include <intuition/intuition.h>
  16.  
  17. typedef struct Screen SCR;
  18. typedef struct Task TASK;
  19.  
  20. extern XPORT *getport();
  21. extern XPORT *init_mwb();
  22.  
  23. long IntuitionBase;
  24.  
  25. main(ac, av)
  26. char *av[];
  27. {
  28.     register int i;
  29.     register char *ptr;
  30.     register XPORT *xport;
  31.     XIT xit;
  32.     int val;
  33.     char hset, wset;
  34.     char err = (ac == 1);
  35.  
  36.     if (xport = (XPORT *)FindPort(PORT_NAME)) {
  37.         xit.screen  = 0;
  38.         xit.scrmodes= xport->xit[0].scrmodes;
  39.         xit.width   = xport->xit[0].width;
  40.         xit.height  = xport->xit[0].height;
  41.         xit.depth   = xport->xit[0].depth;
  42.         xit.flags   = 0;
  43.     }
  44.     hset = wset = 0;
  45.     for (i = 1; !err && i < ac; ++i) {
  46.         ptr = av[i];
  47.         if (*ptr)
  48.             val = atoi(ptr+1);
  49.         switch(*ptr) {
  50.         case 'i':
  51.             if ((xport = (XPORT *)FindPort(PORT_NAME)) && xport->segment) {
  52.                 puts ("MWB already initialized");
  53.                 break;
  54.             }
  55.             xport = (XPORT *)init_mwb();
  56.             xit.scrmodes= xport->xit[0].scrmodes;
  57.             xit.width = xport->xit[0].width;
  58.             xit.height= xport->xit[0].height;
  59.             xit.depth = xport->xit[0].depth;
  60.             operation(getport(), OP_STARTUP, 0, &xit);
  61.             break;
  62.         case 'u':
  63.             if ((xport = FindPort(PORT_NAME)) == NULL || xport->segment == 0) {
  64.                 puts ("MWB does current exist");
  65.                 break;
  66.             }
  67.             uninit_mwb(xport);
  68.             break;
  69.         case 'n':
  70.             operation(getport(), OP_NEWSCREEN, 0, &xit);
  71.             break;
  72.         case 'c':
  73.             operation(getport(), OP_CLOSEUNUSED, 0, &xit);
  74.             break;
  75.         case 'd':
  76.             if (val > 0)
  77.                 xit.depth = val;
  78.             else
  79.                 puts ("bad depth");
  80.             break;
  81.         case 'w':
  82.             if ((val % 16) == 0 && val > 32) {
  83.                 xit.width = val;
  84.                 wset = 1;
  85.             } else {
  86.                 puts ("bad width");
  87.             }
  88.             break;
  89.         case 'h':
  90.             if (val > 32) {
  91.                 xit.height = val;
  92.                 hset = 1;
  93.             } else {
  94.                 puts ("bad height");
  95.             }
  96.             break;
  97.         case 'I':
  98.             if (!(xit.scrmodes & LACE)) {
  99.                 xit.scrmodes |= LACE;
  100.                 if (!hset)
  101.                     xit.height <<= 1;
  102.             }
  103.             break;
  104.         case 'N':
  105.             if (xit.scrmodes & LACE) {
  106.                 xit.scrmodes &= ~LACE;
  107.                 if (!hset)
  108.                     xit.height >>= 1;
  109.             }
  110.             break;
  111.         case 'L':
  112.             if (xit.scrmodes & HIRES) {
  113.                 xit.scrmodes &= ~HIRES;
  114.                 if (!wset)
  115.                     xit.width >>= 1;
  116.             }
  117.             break;
  118.         default:
  119.             if (*ptr < '0' || *ptr > '9') {
  120.                 err = 1;
  121.                 break;
  122.             }
  123.             operation(getport(), OP_SETSCREEN, atoi(ptr), &xit);
  124.             break;
  125.         }
  126.     }
  127.     if (err) {
  128.         puts ("MWB [i/u/c]");
  129.         puts ("MWB #");
  130.         puts ("MWB [L] [N] [I] [h#] [w#] [d#] n");
  131.         puts ("MWB V1.01 (c) 1987 Matthew Dillon,  All Rights Reserved");
  132.         puts ("MWB is freely redistributable");
  133.     }
  134. }
  135.  
  136. /*
  137.  * Send the operation message to the task.
  138.  */
  139.  
  140. operation(taskport, com, screen, xit)
  141. PORT *taskport;
  142. XIT *xit;
  143. {
  144.     XMSG xmsg;
  145.     PORT *port = CreatePort(NULL, 0);
  146.     long scr;
  147.  
  148.     xmsg.msg.mn_Node.ln_Type = NT_MESSAGE;
  149.     xmsg.msg.mn_Node.ln_Pri  = 0;
  150.     xmsg.msg.mn_Node.ln_Name = NULL;
  151.     xmsg.msg.mn_ReplyPort = port;
  152.     xmsg.msg.mn_Length = sizeof(XMSG);
  153.     xmsg.com     = com;
  154.     xmsg.screeno = screen;
  155.     xmsg.xit     = *xit;
  156.     PutMsg(taskport, &xmsg);
  157.     WaitPort(port);
  158.     scr = GetMsg(port);
  159.     DeletePort(port);
  160.     return(xmsg.com);
  161. }
  162.  
  163. /*
  164.  *  Initialize MWB by LoadSeg'ing "mwb_resident" and CreateTask()ing the
  165.  *  code.   mwb_resident will then create the global port.
  166.  *
  167.  */
  168.  
  169. XPORT *
  170. init_mwb()
  171. {
  172.     long segment;
  173.     char *ptr;
  174.     register XPORT *xport;
  175.     register SCR *wscr;
  176.     register TASK *task;
  177.     char addit = 0;
  178.  
  179.     segment = LoadSeg("mwb_resident");
  180.     if (!segment) {
  181.         segment = LoadSeg("c:mwb_resident");
  182.         if (!segment) {
  183.             puts ("Cannot find 'mwb_resident'");
  184.             exit(1);
  185.         }
  186.     }
  187.     IntuitionBase = OpenLibrary("intuition.library", 0);
  188.     if (IntuitionBase == 0) {
  189.         puts ("Unable to open intuition");
  190.         UnLoadSeg(segment);
  191.         exit(1);
  192.     }
  193.     xport = FindPort(PORT_NAME);
  194.     if (xport) {
  195.         puts ("Re-Linking");
  196.     } else {
  197.         addit = 1;
  198.         ptr = (char *)AllocMem(strlen(PORT_NAME)+1, MEMF_PUBLIC);
  199.         xport = (XPORT *)AllocMem(sizeof(XPORT), MEMF_PUBLIC|MEMF_CLEAR);
  200.         strcpy(ptr, PORT_NAME);
  201.         strcpy(xport->scrname, SCREEN_NAME);
  202.         strcpy(xport->taskname,TASK_NAME);
  203.         xport->xit[0].screen = wscr = OpenWorkBench();
  204.         xport->xit[0].scrmodes = wscr->ViewPort.Modes;
  205.         xport->xit[0].width  = wscr->Width;
  206.         xport->xit[0].height = wscr->Height;
  207.         xport->xit[0].depth  = wscr->BitMap.Depth;
  208.         xport->xit[0].flags = FL_NOREMOVE|FL_DEFINED;
  209.         NewList(&xport->port.mp_MsgList);
  210.         xport->port.mp_Node.ln_Type = NT_MSGPORT;
  211.         xport->port.mp_Node.ln_Pri  = 0;
  212.         xport->port.mp_Node.ln_Name = ptr;
  213.     }
  214.     xport->segment = segment;
  215.     task = CreateTask(xport->taskname, 0, (segment<<2)+4, 4096);
  216.     xport->port.mp_SigTask = task;
  217.     xport->port.mp_SigBit = 1;          /*  0x00000002  */
  218.     xport->port.mp_Flags  = PA_SIGNAL;
  219.     if (addit)
  220.         AddPort(xport);                 /*  add port to system lists    */
  221.     Signal(task, 1);                    /*  start the task  0x00000001  */
  222.     CloseLibrary(IntuitionBase);
  223.     puts ("MWB now resident");
  224.     return(xport);
  225. }
  226.  
  227.  
  228. uninit_mwb(xport)
  229. XPORT *xport;
  230. {
  231.     long segment;
  232.     TASK *task;
  233.     XIT xit;
  234.  
  235.     segment = xport->segment;
  236.     task = xport->port.mp_SigTask;
  237.     if (operation(xport, OP_QUIT, 0, &xit) < 0) {
  238.         puts ("Warning: not all screens could be closed");
  239.         xport->segment = 0;
  240.         xport->port.mp_SigTask = NULL;
  241.     } else {
  242.         RemPort(xport);
  243.         FreeMem(xport->port.mp_Node.ln_Name, strlen(xport->port.mp_Node.ln_Name)+1);
  244.         FreeMem(xport, sizeof(XPORT));
  245.     }
  246.     DeleteTask(task);
  247.     UnLoadSeg(segment);
  248.     puts ("MWB now unloaded");
  249. }
  250.  
  251. XPORT *
  252. getport()
  253. {
  254.     XPORT *xport = (XPORT *)FindPort(PORT_NAME);
  255.     if (!xport || xport->segment == 0) {
  256.         puts ("MWB not initialized");
  257.         exit(1);
  258.     }
  259.     return(xport);
  260. }
  261.  
  262.  
  263.  
  264.